package com.gotokeep.keep.composition;

import android.media.MediaCodecInfo;
import android.text.TextUtils;

/**
 * @author xana/cuixianming
 * @version 1.0
 * @since 2018-05-03 16:53
 */
public final class MediaExportConfiguration {
    private String filePath;

    private int width;
    private int height;
    private String videoMimeType;
    private int frameRate;
    private int videoBitRate;
    private int keyFrameInterval;

    private String audioMimeType;
    private int sampleRate;
    private int channelCount;
    private int audioBitRate;
    private int aacProfile;

    private MediaExportConfiguration(Builder builder) {
        filePath = builder.filePath;
        width = builder.width;
        height = builder.height;
        videoMimeType = builder.videoMimeType;
        frameRate = builder.frameRate;
        videoBitRate = builder.videoBitRate;
        keyFrameInterval = builder.keyFrameInterval;
        audioMimeType = builder.audioMimeType;
        sampleRate = builder.sampleRate;
        channelCount = builder.channelCount;
        audioBitRate = builder.audioBitRate;
        aacProfile = builder.aacProfile;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getFilePath() {
        return filePath;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getFrameRate() {
        return frameRate;
    }

    public int getVideoBitRate() {
        return videoBitRate;
    }

    public int getKeyFrameInterval() {
        return keyFrameInterval;
    }

    public String getVideoMimeType() {
        return videoMimeType;
    }

    public String getAudioMimeType() {
        return audioMimeType;
    }

    public int getSampleRate() {
        return sampleRate;
    }

    public int getChannelCount() {
        return channelCount;
    }

    public int getAudioBitRate() {
        return audioBitRate;
    }

    public int getAacProfile() {
        return aacProfile;
    }

    public static final class Builder {
        private static final int DEFAULT_FRAME_RATE = 24;
        private static final int DEFAULT_VIDEO_BIT_RATE = 2000;
        private static final int DEFAULT_I_FRAME_INTERVAL = 10;

        private static final int DEFAULT_AUDIO_SAMPLE_RATE = 11025;
        private static final int DEFAULT_AAC_PROFILE = MediaCodecInfo.CodecProfileLevel.AACObjectLC;
        private static final int DEFAULT_AUDIO_BIT_RATE = 64000;
        private static final int DEFAULT_CHANNEL_COUNT = 1;

        private String filePath;
        private int width;
        private int height;
        private String videoMimeType = "video/avc";
        private int frameRate = DEFAULT_FRAME_RATE;
        private int videoBitRate = DEFAULT_VIDEO_BIT_RATE;
        private int keyFrameInterval = DEFAULT_I_FRAME_INTERVAL;

        private String audioMimeType = "audio/mp4a-latm";
        private int sampleRate = DEFAULT_AUDIO_SAMPLE_RATE;
        private int channelCount = DEFAULT_CHANNEL_COUNT;
        private int audioBitRate = DEFAULT_AUDIO_BIT_RATE;
        private int aacProfile = DEFAULT_AAC_PROFILE;

        private Builder() {
        }

        public Builder setFilePath(String filePath) {
            this.filePath = filePath;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setVideoMimeType(String videoMimeType) {
            this.videoMimeType = videoMimeType;
            return this;
        }

        public Builder setFrameRate(int frameRate) {
            this.frameRate = frameRate;
            return this;
        }

        public Builder setVideoBitRate(int videoBitRate) {
            this.videoBitRate = videoBitRate;
            return this;
        }

        public Builder setKeyFrameInterval(int keyFrameInterval) {
            this.keyFrameInterval = keyFrameInterval;
            return this;
        }

        public Builder setAudioMimeType(String audioMimeType) {
            this.audioMimeType = audioMimeType;
            return this;
        }

        /**
         * 设置音频的输出采样率，必须与源（input）一致
         * @param sampleRate 输出的采样率
         * @return Builder
         */
        public Builder setSampleRate(int sampleRate) {
            this.sampleRate = sampleRate;
            return this;
        }

        /**
         * 设置音频的输出通道数量，必须与源（input）一致
         * @param channelCount 频道数量
         * @return Builder
         */
        public Builder setChannelCount(int channelCount) {
            this.channelCount = channelCount;
            return this;
        }

        public Builder setAudioBitRate(int audioBitRate) {
            this.audioBitRate = audioBitRate;
            return this;
        }

        public Builder setAacProfile(int aacProfile) {
            this.aacProfile = aacProfile;
            return this;
        }

        public MediaExportConfiguration build() {
            if (TextUtils.isEmpty(filePath) || width <= 0 || height <= 0) {
                throw new IllegalArgumentException("invalid build arguments: filePath, width, height must not be empty");
            }
            return new MediaExportConfiguration(this);
        }
    }
}
